package lj.courseinterect.rest.resources;

import lj.courseinterect.dao.i.CourseInfoDao;
import lj.courseinterect.dao.i.NoticeInfoDao;
import lj.courseinterect.dao.i.StudentDao;
import lj.courseinterect.entity.CourseInfo;
import lj.courseinterect.entity.NoticeInfo;
import lj.courseinterect.entity.Student;
import lj.courseinterect.entity.Teacher;
import lj.courseinterect.transfer.OperationTransfer;
import lj.courseinterect.transfer.StudentTransfer;
import lj.courseinterect.transfer.TeacherTransfer;
import lj.courseinterect.transfer.TokenTransfer;
import lj.courseinterect.utils.TokenUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
@Path("/{a:tea|stu}/courses")
public class CourseInfoResource
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CourseInfoDao courseInfoDao;

    @Autowired
    private NoticeInfoDao noticeInfoDao;

    @Autowired
    private TeacherResource teacherResource;


    public Logger getLogger() {
        return logger;
    }

    public CourseInfoDao getCourseInfoDao() {
        return courseInfoDao;
    }

    public void setCourseInfoDao(CourseInfoDao courseInfoDao) {
        this.courseInfoDao = courseInfoDao;
    }

    public TeacherResource getTeacherResource() {
        return teacherResource;
    }

    public void setTeacherResource(TeacherResource teacherResource) {
        this.teacherResource = teacherResource;
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OperationTransfer<CourseInfo> create(CourseInfo courseInfo)
    {
        this.logger.info("create(): " + courseInfo.getCouName());
        OperationTransfer<CourseInfo> operationTransfer;

        courseInfo.setCouTime(new Date());
        courseInfo.setCouState(CourseInfo.CouState.NORMAL);

        Teacher teacher = teacherResource.getTeacher();
        if (teacher == null){
            operationTransfer = new OperationTransfer<CourseInfo>(false, "教师不存在或者未登录");
        } else {
            courseInfo.setTeaId(teacher.getTeaId());
            try{
                courseInfo = this.courseInfoDao.save(courseInfo);
                operationTransfer = new OperationTransfer<CourseInfo>(true, "添加课程信息成功", courseInfo);
            } catch (Exception ex){
                operationTransfer = new OperationTransfer<CourseInfo>(false, "添加课程信息失败");
                ex.printStackTrace();
            }
        }


        this.logger.info("create(): " + operationTransfer.toString());
        return operationTransfer;
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<CourseInfo> queryByTeaId(){
            Teacher teacher = teacherResource.getTeacher();
        List<CourseInfo> courseInfos;
        if (teacher == null){
            courseInfos = null;
        } else {
            courseInfos = courseInfoDao.queryByTeaId(teacher.getTeaId());
        }

        return courseInfos;
    }

    @GET
    @Path("/{couId: [0-9]*$}")
    @Produces(MediaType.APPLICATION_JSON)
    public CourseInfo getCourse(@PathParam("couId") Long couId){
        CourseInfo courseInfo = courseInfoDao.find(couId);

        return courseInfo;
    }

    @GET
    @Path("getCourseByName/{couName}")
    @Produces(MediaType.APPLICATION_JSON)
    public CourseInfo getCourseByName(@PathParam("couName") String couName){
        return courseInfoDao.findByCouName(couName);
    }

    @POST
    @Path("/{couId: [0-9]*$}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public OperationTransfer<CourseInfo> update(@PathParam("couId") Long couId, CourseInfo courseInfo)
    {
        this.logger.info("update(): " + courseInfo.getCouName());
        OperationTransfer<CourseInfo> operationTransfer;

        Teacher teacher = teacherResource.getTeacher();
        if (teacher == null){
            operationTransfer = new OperationTransfer<CourseInfo>(false, "教师不存在或者未登录");
        } else {
//            courseInfo.setTeaId(teacher.getTeaId());
            if(!teacher.getTeaId().equals(courseInfo.getTeaId()) || !couId.equals(courseInfo.getCouId())){
                // 课程不属于老师
                operationTransfer = new OperationTransfer<CourseInfo>(false, "权限错误");
            } else {
                try{
                    courseInfo = this.courseInfoDao.save(courseInfo);
                    operationTransfer = new OperationTransfer<CourseInfo>(true, "修改课程信息成功", courseInfo);
                } catch (Exception ex){
                    operationTransfer = new OperationTransfer<CourseInfo>(false, "修改课程信息失败");
                    ex.printStackTrace();
                }
            }
        }

        this.logger.info("update(): " + operationTransfer.toString());
        return operationTransfer;
    }
}